package com.youdoneed.news.news.service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.youdoneed.news.news.dao.CmfNavMapper;
import com.youdoneed.news.news.dao.CmfNavRelationMapper;
import com.youdoneed.news.news.entity.CmfNavRelation;
import com.youdoneed.news.news.vo.CmfNavVo;
import com.youdoneed.user.feign.UserFeignClient;
import com.youdoneed.user.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CmfNavService {

    private final Logger LOGGER = LoggerFactory.getLogger(CmfNavService.class);

    @Autowired
    private CmfNavMapper cmfNavMapper;
    @Autowired
    private CmfNavRelationMapper cmfNavRelationMapper;
    @Autowired
    private UserFeignClient userFeignClient;

    /**
     * 获取用户关注的新闻导航
     *
     * @param userId
     * @return
     */
    public List<CmfNavVo> getFocusNewsNavcation(String userId) {
        //获取用户信息
        User user = userFeignClient.getUser(userId).getData();
        Long bornDate = (user.getBornDate() == null) ? null : user.getBornDate().getTime();
        Integer gender = user.getGender();
        Integer navcId = getNavCidByGender(gender, bornDate);
        List<CmfNavVo> newsNavList = new ArrayList<CmfNavVo>();
        newsNavList = cmfNavMapper.getAllNewsNav(navcId);
        List<CmfNavRelation> navRelList = cmfNavRelationMapper.getNewsNavRelList(userId);
        //有关注导航时显示关注导航
        if (null != navRelList && navRelList.size() > 0) {
            List<CmfNavVo> myNewsNav = new ArrayList<CmfNavVo>();
            for (CmfNavRelation navRelation : navRelList) {
                Integer navId = navRelation.getNavId();
                CmfNavVo cmfNavVo = cmfNavMapper.getFocusNewsNav(navId);
                if (null != cmfNavVo) {
                    myNewsNav.add(cmfNavVo);
                }
            }
            newsNavList = myNewsNav;
        }
        List<CmfNavVo> subNavs = new ArrayList<>();
        //查询子导航
        for (CmfNavVo cmfNav : newsNavList) {
            subNavs = cmfNavMapper.getSubNewsNav(cmfNav.getId());
            if (null != subNavs && subNavs.size() > 0) {
                cmfNav.setSubNavs(subNavs);
            }
        }
        return newsNavList;
    }

    /**
     * 根据年龄和性别确定导航
     *
     * @param gender
     * @param bornDate
     * @return
     */
    private Integer getNavCidByGender(Integer gender, Long bornDate) {
        if (gender == 1 && 473356800000L >= bornDate) {
            return 4;
        }
        if (gender == 1 && 788889600000L >= bornDate && 473356800000L < bornDate) {
            return 6;
        }
        if (gender == 1 && 788889600000L < bornDate) {
            return 7;
        }
        if (gender == 2 && 473356800000L >= bornDate) {
            return 5;
        }
        if (gender == 2 && 788889600000L >= bornDate && 473356800000L < bornDate) {
            return 11;
        }
        if (gender == 2 && 788889600000L < bornDate) {
            return 10;
        }
        if (gender == 0) {
            return 1;
        }
        return 1;
    }

    /**
     * 新闻频道编辑查询
     *
     * @param userId
     * @return
     */
    public Map<String, List<CmfNavVo>> getEditNewNavData(String userId) {
        //获取用户信息
        User user = userFeignClient.getUser(userId).getData();
        Long bornDate = user.getBornDate().getTime();
        Integer gender = user.getGender();
        Integer navcId = getNavCidByGender(gender, bornDate);
        Map<String, List<CmfNavVo>> map = new HashMap<String, List<CmfNavVo>>();
        List<CmfNavRelation> relList = cmfNavRelationMapper.getNewsNavRelList(userId);
        List<CmfNavVo> myNewsNav = new ArrayList<CmfNavVo>();
        //未关注的列表
        List<CmfNavVo> rcdNewsNav = cmfNavMapper.getAllNewsNav(navcId);
        LOGGER.info("rcdNewsNav.size----------" + rcdNewsNav.size());
        if (null != relList && relList.size() > 0) {
            for (CmfNavRelation navRelation : relList) {
                Integer navId = navRelation.getNavId();
                myNewsNav.add(cmfNavMapper.getFocusNewsNav(navId));
            }
            if (null != myNewsNav && myNewsNav.size() > 0) {
                rcdNewsNav.removeAll(myNewsNav);
            }
            LOGGER.info("rcdNewsNav.size----------" + rcdNewsNav.size());
        }
        map.put("myNewsNav", myNewsNav);
        map.put("rcdNewsNav", rcdNewsNav);
        return map;
    }

    /**
     * 新闻频道编辑
     *
     * @param userId
     * @param data
     */
    @Transactional(rollbackFor = Throwable.class)
    public void editNewsNavData(String userId, String data) {
        //若全部取消关注，直接删除
        if (null == data || "[]".equals(data)) {
            //删除用户关注记录
            cmfNavRelationMapper.deleteByUid(userId);
            return;
        }
        //清空用户关注记录
        cmfNavRelationMapper.deleteByUid(userId);
        //批量更新关注记录
        Gson gson = new Gson();
        List<CmfNavVo> cmfNavList = gson.fromJson(data, new TypeToken<List<CmfNavVo>>() {
        }.getType());

        List<CmfNavRelation> relationList = new ArrayList<CmfNavRelation>();
        //获取时间
        String time = String.valueOf(System.currentTimeMillis() / 1000);

        for (int i = 0; i < cmfNavList.size(); i++) {
            CmfNavVo cmfNav = cmfNavList.get(i);
            CmfNavRelation relation = new CmfNavRelation();
            relation.setAddTime(Integer.valueOf(time));
            relation.setListorder(i);
            relation.setFaceUid(userId);
            relation.setNavId(cmfNav.getId());
            relationList.add(relation);
        }
        cmfNavRelationMapper.insertBatch(relationList);
    }

    /**
     * 获取子导航列表
     *
     * @return
     */
    public List<CmfNavVo> getSubNewsNav(Integer parentId) {
        return cmfNavMapper.getSubNewsNav(parentId);
    }

}
